package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Scanner;

public class ReflectDemo3 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入类名:");
        try {
            String className = sc.nextLine();
            Class class1 = Class.forName(className);
            Constructor cs = class1.getConstructor();
            Object obj1 = cs.newInstance();

            System.out.println(obj1);

            Constructor cs2 = class1.getConstructor(String.class);
            Object object2 = cs2.newInstance("李浩");

            System.out.println(object2);

            Constructor cs3 = class1.getConstructor(String.class,int.class);
            Object object3 = cs3.newInstance("李浩",23);
            System.out.println(object3);



        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
